package cn.itsource.org.controller;

import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"员工接口"})
@RestController  //@Controller(交给spring管理) + @ResponseBody(方法返回json格式)
@RequestMapping("/emp")
public class EmployeeController {

    /**
     * 注入业务层依赖
     */
    @Autowired
    private IEmployeeService service;

    /**
     * 查询所有员工
     */
    @GetMapping
    private JsonResult findAll(){
        //1.查询所有
        List<Employee> list = service.findAll();
        //2.封装对象后返回
        return JsonResult.createSuccess(list);
    }

    /**
     * 查询一个员工
     */
    @GetMapping("/findOne/{id}")
    private JsonResult findOne(@PathVariable("id") Long id){
        Employee serviceOne = service.findOne(id);
        return JsonResult.createSuccess(serviceOne);
        //可简写
//        return JsonResult.createSuccess(service.findOne(id));
    }

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    public PageList<Employee> selectPage(@RequestBody EmployeeQuery query){
        return service.selectPage(query);
    }

    /**
     * 新增或修改的联合方法
     */
    @PostMapping
    @ApiOperation(value = "新增或修改员工",notes = "芒果")
    public JsonResult saveOrUpdate(@ApiParam(value = "新增或修改员工参数",required = true) @RequestBody Employee employee){
        try {
            service.saveOrUpdate(employee);
            return JsonResult.creatSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.creatError("系统异常,请联系笨蛋程序员~");
        }
    }

    /**
     * 根据id删除一个员工
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            service.deleteById(id);
            return JsonResult.creatSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.creatError("系统异常,请联系笨蛋程序员~");
        }
    }

    /**
     * 查询部门经理
     */
    @GetMapping("/findManagers")
    public JsonResult findManagers(){
        //1.查询所有
        List<Employee> managers = service.findManagers();
        //2.封装对象返回
        return JsonResult.createSuccess(managers);
    }

}
